﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Xml.Linq;
using Lorow.SampleData;
using Lorow.ViewModels;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace Lorow
{
    public partial class PageTwo : PhoneApplicationPage
    {
        XDocument XDoc;
        public PageTwo()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string strItemIndex;

            if (NavigationContext.QueryString.TryGetValue("goto", out strItemIndex))

                pivot.SelectedItem = pivot.Items[Convert.ToInt32(strItemIndex)];
        }
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.AllDataModel.IsDataLoaded)
            {
                App.AllDataModel.LoadData();
            }
            HomeListBox.ItemsSource = App.AllDataModel.Home;
            DapurListBox.ItemsSource = App.AllDataModel.Sedap;
            FurnitureListBox.ItemsSource = App.AllDataModel.Busana;
            TamanListBox.ItemsSource = App.AllDataModel.Kecantikan;
            TidurListBox.ItemsSource = App.AllDataModel.News;
            SelebritiListBox.ItemsSource = App.AllDataModel.Selebriti;
            ProfilListBox.ItemsSource = App.AllDataModel.Profil;
            KesehatanListBox.ItemsSource = App.AllDataModel.Kesehatan;
            KeluargaListBox.ItemsSource = App.AllDataModel.Keluarga;
            KarierListBox.ItemsSource = App.AllDataModel.Karier;
            GriyaListBox.ItemsSource = App.AllDataModel.Griya;
            TipsListBox.ItemsSource = App.AllDataModel.Tips;
            GadgetListBox.ItemsSource = App.AllDataModel.Gadget;
        }
        private void HomeListBoxChange(object sender, SelectionChangedEventArgs e)
        {
            var guid = ((ItemViewModel)HomeListBox.SelectedItem).Id;
            // Navigate to the new page
            PhoneApplicationService.Current.State["Item"] = HomeListBox.SelectedItem;
            NavigationService.Navigate(new Uri("/NewsViewer.xaml", UriKind.Relative));
        }
        private void DapurListBoxChange(object sender, SelectionChangedEventArgs e)
        {
            var guid = ((ItemViewModel)DapurListBox.SelectedItem).Id;
            // Navigate to the new page
            PhoneApplicationService.Current.State["Item"] = DapurListBox.SelectedItem;
            NavigationService.Navigate(new Uri("/NewsViewer.xaml", UriKind.Relative));
        }
        private void FurnitureListBoxChange(object sender, SelectionChangedEventArgs e)
        {
            var guid = ((ItemViewModel)FurnitureListBox.SelectedItem).Id;
            // Navigate to the new page
            PhoneApplicationService.Current.State["Item"] = FurnitureListBox.SelectedItem;
            NavigationService.Navigate(new Uri("/NewsViewer.xaml", UriKind.Relative));
        }
        private void TamanListBoxChange(object sender, SelectionChangedEventArgs e)
        {
            var guid = ((ItemViewModel)TamanListBox.SelectedItem).Id;
            // Navigate to the new page
            PhoneApplicationService.Current.State["Item"] = TamanListBox.SelectedItem;
            NavigationService.Navigate(new Uri("/NewsViewer.xaml", UriKind.Relative));
        }
        private void TidurListBoxChange(object sender, SelectionChangedEventArgs e)
        {
            var guid = ((ItemViewModel)TidurListBox.SelectedItem).Id;
            // Navigate to the new page
            PhoneApplicationService.Current.State["Item"] = TidurListBox.SelectedItem;
            NavigationService.Navigate(new Uri("/NewsViewer.xaml", UriKind.Relative));
        }
        private void SelebritiListBoxChange(object sender, SelectionChangedEventArgs e)
        {
            var guid = ((ItemViewModel)SelebritiListBox.SelectedItem).Id;
            // Navigate to the new page
            PhoneApplicationService.Current.State["Item"] = SelebritiListBox.SelectedItem;
            NavigationService.Navigate(new Uri("/NewsViewer.xaml", UriKind.Relative));
        }
        private void ProfilListBoxChange(object sender, SelectionChangedEventArgs e)
        {
            var guid = ((ItemViewModel)ProfilListBox.SelectedItem).Id;
            // Navigate to the new page
            PhoneApplicationService.Current.State["Item"] = ProfilListBox.SelectedItem;
            NavigationService.Navigate(new Uri("/NewsViewer.xaml", UriKind.Relative));
        }
        private void KesehatanListBoxChange(object sender, SelectionChangedEventArgs e)
        {
            var guid = ((ItemViewModel)KesehatanListBox.SelectedItem).Id;
            // Navigate to the new page
            PhoneApplicationService.Current.State["Item"] = KesehatanListBox.SelectedItem;
            NavigationService.Navigate(new Uri("/NewsViewer.xaml", UriKind.Relative));
        }
        private void KeluargaListBoxChange(object sender, SelectionChangedEventArgs e)
        {
            var guid = ((ItemViewModel)KeluargaListBox.SelectedItem).Id;
            // Navigate to the new page
            PhoneApplicationService.Current.State["Item"] = KeluargaListBox.SelectedItem;
            NavigationService.Navigate(new Uri("/NewsViewer.xaml", UriKind.Relative));
        }
        private void KarierListBoxChange(object sender, SelectionChangedEventArgs e)
        {
            var guid = ((ItemViewModel)KarierListBox.SelectedItem).Id;
            // Navigate to the new page
            PhoneApplicationService.Current.State["Item"] = KarierListBox.SelectedItem;
            NavigationService.Navigate(new Uri("/NewsViewer.xaml", UriKind.Relative));
        }
        private void GriyaListBoxChange(object sender, SelectionChangedEventArgs e)
        {
            var guid = ((ItemViewModel)GriyaListBox.SelectedItem).Id;
            // Navigate to the new page
            PhoneApplicationService.Current.State["Item"] = GriyaListBox.SelectedItem;
            NavigationService.Navigate(new Uri("/NewsViewer.xaml", UriKind.Relative));
        }
        private void TipsListBoxChange(object sender, SelectionChangedEventArgs e)
        {
            var guid = ((ItemViewModel)TipsListBox.SelectedItem).Id;
            // Navigate to the new page
            PhoneApplicationService.Current.State["Item"] = TipsListBox.SelectedItem;
            NavigationService.Navigate(new Uri("/NewsViewer.xaml", UriKind.Relative));
        }
        private void GadgetListBoxChange(object sender, SelectionChangedEventArgs e)
        {
            var guid = ((ItemViewModel)GadgetListBox.SelectedItem).Id;
            // Navigate to the new page
            PhoneApplicationService.Current.State["Item"] = GadgetListBox.SelectedItem;
            NavigationService.Navigate(new Uri("/NewsViewer.xaml", UriKind.Relative));
        }
        private void pin()
        {
            ShellTile Tile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("goto=" + pivot.SelectedIndex));
            if (Tile != null) Tile.Delete();
            //
            // create a new secondary tile
            StandardTileData data = new StandardTileData();
            // tile foreground data
            if(pivot.SelectedIndex==0)
                data.Title = "LOROW HOME";
            else if (pivot.SelectedIndex == 1)
                data.Title = "LOROW NEWS";
            else if (pivot.SelectedIndex == 2)
                data.Title = "LOROW SEDAP";
            else if (pivot.SelectedIndex == 3)
                data.Title = "LOROW BUSANA";
            else if (pivot.SelectedIndex == 4)
                data.Title = "LOROW KECANTIKAN";
            else if (pivot.SelectedIndex == 5)
                data.Title = "LOROW SELEBRITI";
            else if (pivot.SelectedIndex == 6)
                data.Title = "LOROW PROFIL";
            else if (pivot.SelectedIndex == 7)
                data.Title = "LOROW KESEHATAN";
            else if (pivot.SelectedIndex == 8)
                data.Title = "LOROW KELUARGA";
            else if (pivot.SelectedIndex == 9)
                data.Title = "LOROW KARIER";
            else if (pivot.SelectedIndex == 10)
                data.Title = "LOROW GRIYA";
            else if (pivot.SelectedIndex == 11)
                data.Title = "LOROW TIPS";
            else if (pivot.SelectedIndex == 12)
                data.Title = "LOROW GADGET";
            data.BackgroundImage = new Uri("/Images/Blue.jpg", UriKind.Relative);
            data.Count = 10;
            // create a new tile for this Second Page
            ShellTile.Create(new Uri("/GroupTwo.xaml?goto=" + pivot.SelectedIndex, UriKind.Relative), data);
        }
        private void aboutClickHandler(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/About.xaml", UriKind.Relative));
        }
        private void shareClickHandler(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/FacebookPage.xaml?global=yes", UriKind.Relative));
        }
        private void tileClickHandler(object sender, EventArgs e)
        {
            pin();
        }
        private void searchClickHandler(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/SearchResult.xaml", UriKind.Relative));
        }
        private void bookmarksClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Home.xaml?goto=1", UriKind.Relative));
        }
        private void recentClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Home.xaml?goto=2", UriKind.Relative));
        }
        private void infoClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Home.xaml?goto=3", UriKind.Relative));

        }

        private void appbar_StateChanged_1(object sender, ApplicationBarStateChangedEventArgs e)
        {

        }

        private void ApplicationBarMenuItem_Click_1(object sender, EventArgs e)
        {

        }

    }
}